package com.jzo2o.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.jzo2o.market.constants.RedisConstants;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.dto.response.SeizeCouponInfoResDTO;
import com.jzo2o.market.service.ISeizeActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 领取优惠券服务实现类
 * @author: jzo2o
 **/
@Service
@Slf4j
public class ISeizeActivityServiceImpl implements ISeizeActivityService {
    @Resource
    private RedisTemplate redisTemplate;


    /**
     * @description: 查询领取优惠券活动列表
     * @param: tabType
     * @return: java.util.List<com.jzo2o.market.model.dto.response.SeizeCouponInfoResDTO>
     **/
    @Override
    public List<SeizeCouponInfoResDTO> queryList(Integer tabType) {
        log.info("开始查询领取优惠券活动列表");
        //reids 查询全部符合条件优惠券活动
        Object activityObject = redisTemplate.opsForValue().get(RedisConstants.RedisKey.ACTIVITY_CACHE_LIST);
        if (activityObject == null) {
            return new ArrayList<SeizeCouponInfoResDTO>();
        }

        // 转换成优惠券活动列表对象
        List<Activity> activityList = JSONUtil.toList(JSONUtil.toJsonStr(activityObject), Activity.class);
        //判断最新状态并更新
        for (Activity activity : activityList) {
            activitySetNewStatus(activity);
        }
        //根据条件进行过滤
        int query = tabType == 1 ? ActivityStatusEnum.DISTRIBUTING.getStatus() : ActivityStatusEnum.NO_DISTRIBUTE.getStatus();
        List<Activity> queryList = activityList.stream().filter(s -> s.getStatus() == query).collect(Collectors.toList());
        //转换为返回对象
        ArrayList<SeizeCouponInfoResDTO> seizeCouponInfoResDTOS = new ArrayList<>();

        for (Activity activity : queryList) {
            SeizeCouponInfoResDTO seizeCouponInfoResDTO = BeanUtil.copyProperties(activity, SeizeCouponInfoResDTO.class);
            seizeCouponInfoResDTO.setRemainNum(activity.getStockNum());
            seizeCouponInfoResDTOS.add(seizeCouponInfoResDTO);
        }
        return seizeCouponInfoResDTOS;
    }

    /**
     * @description: 设置优惠券活动最新状态
     * @param: activity
     * @return: void
     **/
    private void activitySetNewStatus(Activity activity) {
        //如果 开始时间 大于或等于当前时间 则设置为进行中
        if (activity.getDistributeStartTime().isBefore(LocalDateTime.now()) || activity.getDistributeStartTime().equals(LocalDateTime.now())) {
            activity.setStatus(ActivityStatusEnum.DISTRIBUTING.getStatus());
        }
        //如果 结束时间 小于当前时间 则设置为已结束
        if (activity.getDistributeEndTime().isBefore(LocalDateTime.now())) {
            activity.setStatus(ActivityStatusEnum.LOSE_EFFICACY.getStatus());
        }
    }
}
